package com.real8.plusplayground.client.activity;

import com.google.api.gwt.services.plus.shared.model.Activity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.event.dom.client.KeyPressEvent;

public class ActivityReadViewImpl extends Composite implements ActivityReadView {
	/**
	 * Driver for this view.
	 */
	interface Driver extends
			SimpleBeanEditorDriver<Activity, ActivityReadViewImpl> {
	}

	private static ActivityReadViewImplUiBinder uiBinder = GWT
			.create(ActivityReadViewImplUiBinder.class);

	interface ActivityReadViewImplUiBinder extends
			UiBinder<Widget, ActivityReadViewImpl> {
	}

	private final Driver driver = GWT.create(Driver.class);

	private Presenter presenter;

	@UiField
	TextBox idEditor;

	@UiField
	Label urlEditor;

	@UiField
	Label titleEditor;

	@UiField
	Label publishedEditor;

	@UiField
	Label updatedEditor;

	public ActivityReadViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		driver.initialize(this);
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;
	}

	@Override
	public SimpleBeanEditorDriver<Activity, ?> getEditorDriver() {
		return driver;
	}

	@Override
	public void setId(String activityId) {
		idEditor.setValue(activityId);
	}

	@UiHandler("idEditor")
	void onIdEditorKeyPress(KeyPressEvent event) {
		// check for return
		int keyCode = event.getNativeEvent().getKeyCode();
		if (keyCode == 13) {
			doIdChange();
		}
	}

	private void doIdChange() {
		if (presenter != null) {
			String activityId = idEditor.getValue();

			presenter.onIdChange(activityId);
		}
	}
}
